package com.paomi.client.fragment.collection.presenter;

import android.app.Activity;
import android.os.Handler;

import com.paomi.client.R;
import com.paomi.client.base.BaseLoadMorePresenter;
import com.paomi.client.base.BaseMvpLCEView;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.bean.CollectionListInfo;
import com.paomi.client.bean.CpllectionInfo;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by pc on 2019/1/29
 * Describe:收藏的演出接口
 * com.paomi.client.fragment.collection.presenter
 */
public class CollectShowPresenter extends BaseLoadMorePresenter<BaseMvpLCEView> {

    private List<CpllectionInfo> list = new ArrayList<>();
    private String type=null;
    private int pageNum=1;
    private Activity activity;
    public void setCollectData(Activity activity,String type, int page){
        this.activity=activity;
        this.type=type;
        pageNum=page;
        loadData(true);
    }
    @Override
    protected void loadData(int page, int skip, boolean pullToRefresh) {
        if(type!=null){
            Api.getInstanceService(Constants.Ticket_URL).getAddFavor(type,""+pageNum,""+Constants.PAGE_SIZE).enqueue(new BaseCallback<CollectionListInfo>() {
                @Override
                public void onResponse(Call<CollectionListInfo> call, CollectionListInfo result) {
                    if (result.getReturnData().getData().size() > 0) {
                        if (pageNum == 1) {
                            list.clear();
                        }
                        if (list.size() > 0) {
                            boolean isData = false;
                            List<CpllectionInfo> listData = result.getReturnData().getData();
                            //判断是否有重复数据
                            for (CpllectionInfo cpllectionInfo : listData) {
                                for (int i = 0; i < list.size(); i++) {
                                    if (cpllectionInfo.getTypeId() == list.get(i).getTypeId()) {
                                        isData = false;
                                        break;
                                    } else {
                                        isData = true;
                                    }

                                }
                            }
                            if (isData) {
                                list.addAll(result.getReturnData().getData());
                            } else {
                                ToastUtil.getToastEmail().ToastShow(activity, null, 1, R.layout.toast_view, activity.getResources().getString(R.string.data_null));
                            }
                        } else {
                            list.addAll(result.getReturnData().getData());
                        }
                        getView().setData(list,false);
                    } else {
                        getView().setData(list,true);
                    }
                }
            });
        }else{
            getView().setData(list,false);
        }

    }

    /**
     * 取消收藏
     */
    public void removeCollection(Map map, int pos) {
        Api.getInstanceService(Constants.Ticket_URL).getaddCollection(map).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        EventBus.getDefault().post(result);
                    }
                }, 100);
            }
        });
    }
}
